package com.zhe.zheblogmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhe.zheblogmaster.config.BlogConstants;
import com.zhe.zheblogmaster.entity.Blog;
import com.zhe.zheblogmaster.entity.BlogComment;
import com.zhe.zheblogmaster.mapper.BlogCommentMapper;
import com.zhe.zheblogmaster.service.BlogCommentService;
import com.zhe.zheblogmaster.util.ZheBlogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WHAOU
 */
@Service
public class BlogCommentServiceImpl implements BlogCommentService {
    @Autowired
    private BlogCommentMapper commentMapper;

    @Override
    public PageInfo<BlogComment> getCommentPage(Integer pageNum) {
        PageHelper.startPage(pageNum, BlogConstants.DEFAULT_PAGE_SIZE);
        List<BlogComment> commentList = commentMapper.selectByMap(null);
        return new PageInfo<>(commentList);
    }

    @Override
    public Integer getTotalComments() {
        return commentMapper.selectTotalOfComment(null);
    }

    @Override
    public Boolean addComment(BlogComment comment) {
        return commentMapper.insertSelective(comment) > 0;
    }

    @Override
    public PageInfo<BlogComment> getCommentPageByBlogId(Long blogId, Integer pageNum) {
        // 封装查询条件 博客id 评论审核状态
        Map<String, Long> params = new HashMap<>(2);
        params.put("blogId", blogId);
        // 设置评论审核状态，过滤审核通过的评论
        params.put("commentStatus", 1L);

        PageHelper.startPage(pageNum, BlogConstants.DEFAULT_PAGE_SIZE);
        List<BlogComment> commentList = commentMapper.selectByMap(params);
        return new PageInfo<>(commentList);
    }

    @Override
    public int check(Integer[] ids) {
        return commentMapper.updateStatusBatch(ids);
    }

    @Override
    public int deleteByIdBatch(Integer[] ids) {
        return commentMapper.deleteByIdBatch(ids);
    }

    @Override
    public int reply(Integer id, String reply) {
        BlogComment comment = commentMapper.selectByCommentId(id);
        if (comment != null && comment.getCommentStatus() == 1) {
            comment.setReplyBody(reply);
            comment.setReplyCreateTime(ZheBlogUtils.getNow());
            return commentMapper.updateByCommentIdSelective(comment);
        }
        return 0;
    }
}
